<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >

      <el-form-item label="产品图片" prop="productImg">
        <UploadImg v-model="formData.productImg" />
      </el-form-item>

      <el-form-item label="准入名称" prop="productName">
        <el-input v-model="formData.productName" maxlength="75" placeholder="请输入产品准入名称" />
      </el-form-item>

      <el-form-item label="产品类型" prop="publicityTypeId">

        <el-select v-model="formData.publicityTypeId" placeholder="请选择">
          <el-option
            v-for="item in productTypeList"
            :key="item.id"
            :label="item.typeNames"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="申请流程" prop="procedures">
<!--        <el-input v-model="formData.procedure" placeholder="请输入产品准入名称" />-->
        <el-select v-model="formData.procedures" placeholder="请选择">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.APPLICATION_PROCEDURE)"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="还款周期" prop="deadline">
        <el-input v-model="formData.deadline" type="number" placeholder="请输入还款周期" />
      </el-form-item>

      <el-form-item label="还款方式" prop="modeOfRepayment">
        <!--        <el-input v-model="formData.modeOfRepayment" placeholder="请输入还款方式" />-->
        <el-select v-model="formData.modeOfRepayment" placeholder="请选择">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MODE_OF_REPAYMENT)"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="年化利率" prop="timeRate">
        <el-input v-model="formData.timeRate" oninput ="value=value.replace(/[^0-9.]/g,'')"   placeholder="请输入年化利率1~100" />
      </el-form-item>

      <el-form-item label="* 产品倍数" v-show="formData.publicityTypeId=='2' || formData.publicityTypeId=='4'
      ||formData.publicityTypeId=='8' || formData.publicityTypeId=='9'
      ||formData.publicityTypeId=='10' || formData.publicityTypeId=='11' || formData.publicityTypeId=='12'
      || formData.publicityTypeId=='13'">
        <el-input v-model="formData.multiple" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入产品倍数" />
      </el-form-item>

      <el-form-item label="* 产品成数X" prop="portions" v-show="formData.publicityTypeId=='1' || formData.publicityTypeId == '5'
      || formData.publicityTypeId=='7' || formData.publicityTypeId=='8'
      || formData.publicityTypeId=='9' ||formData.publicityTypeId=='10'
      || formData.publicityTypeId=='14' || formData.publicityTypeId=='15'
      || formData.publicityTypeId=='16'">
        <el-input v-model="formData.portions" oninput ="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入产品成数" />
      </el-form-item>

      <el-form-item label="银行/机构" prop="bank">
        <!--        <el-input v-model="formData.modeOfRepayment" placeholder="请输入还款方式" />-->
        <el-select v-model="formData.bank" placeholder="请选择" clearable>
          <el-option
            v-for="bank in productBankList"
            :key="bank"
            :label="bank"
            :value="bank"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="最高额度" prop="maxAmount">
        <el-input v-model="formData.maxAmount" oninput ="value=value.replace(/[^0-9.]/g,'')"   placeholder="请输入最高额度（单位：元）" />
      </el-form-item>
<!--      <el-form-item label="收费标准" prop="rates">-->
<!--        <el-input v-model="formData.rates" oninput ="value=value.replace(/[^0-9.]/g,'')"  placeholder="请输入收费标准" />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="公示表id" prop="publicityTypeId">-->
<!--        <el-input v-model="formData.publicityTypeId" placeholder="请输入公示表id" />-->
<!--      </el-form-item>-->

    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as ProductAccessApi from '@/api/hutai/productAccess'
import * as PublicityTypeApi from "@/api/hutai/publicityType";
import { DICT_TYPE, getIntDictOptions } from "@/utils/dict";
import Vue from "@vitejs/plugin-vue";

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  productImg: undefined,
  productName: undefined,
  publicityTypeId: undefined,
  procedures: undefined,
  modeOfRepayment: undefined,
  timeRate: undefined,
  deadline:undefined,
  portions: undefined,
  multiple: undefined,
  rates:undefined,
  maxAmount: undefined,
  bank: undefined,
})
const formRules = reactive({
  productName: [{ required: true, message: '产品准入名称不能为空', trigger: 'blur' }],
  publicityTypeId: [{ required: true, message: '产品类型不能为空', trigger: 'blur' }],
  procedures: [{ required: true, message: '申请流程不能为空', trigger: 'blur' }],
  modeOfRepayment: [{ required: true, message: '还款方式不能为空', trigger: 'blur' }],
  timeRate: [{ required: true, message: '年化利率不能为空', trigger: 'blur' }],
  deadline: [{ required: true, message: '还款周期不能为空', trigger: 'blur' }],
})

// let bankStr = "中国银行,农业银行,工商银行,建设银行,交通银行,重庆银行,农村商业银行,重庆三峡银行,邮储银行,兴业银行,中信银行,厦门银行,民生银行,光大银行,汉口银行,平安银行,哈尔滨银行,华夏银行,成都银行,广发银行,招商银行,渤海银行,富民银行,富滇银行,南粤银行,中银富登村镇银行,浙商银行,大渡口融兴村镇银行,沙坪坝融兴村镇银行,九龙坡民泰村镇银行,渝北银座村镇银行,长运小贷,绿地小贷,亚馨小贷,华夏易达金,度小满,人人信,人保,新大陆,兴业消费金融,锦城消费金融,亚联财,中银消费金融,平安担保,大连银行,浦东发展银行,苏宁银行,泸州银行,其他"
// let optionsBank = bankStr.split(',')

const formRef = ref() // 表单 Ref
const productTypeList = ref([]) // 类型列表
const bank = ref()
const productBankList = ref([]) // 银行机构列表
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProductAccessApi.getProductAccess(id)
    } finally {
      formLoading.value = false
    }
  }
  bank.value = await ProductAccessApi.getConfigDataByKey('admin.product.bank');
  let bankStr = bank.value
  productBankList.value =bankStr.split(',')
  // 类型列表
  productTypeList.value = await PublicityTypeApi.getPublicityTypeList();
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProductAccessApi.ProductAccessVO
    if (formType.value === 'create') {
      await ProductAccessApi.createProductAccess(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProductAccessApi.updateProductAccess(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}


/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    productName: undefined,
    publicityTypeId: undefined
  }
  formRef.value?.resetFields()
}
</script>
